<template>
	<u-modal :show="show" title="下载中" @confirm="show = false" @close="show = false">
		<u-line-progress :percentage="percent" activeColor="#1534ce"></u-line-progress>
	</u-modal>
</template>
<script>
	export default {
		data() {
			return {
				show: false,
				percent: 0
			}
		},
		props: {
			file_path: {
				type: [String],
				default: ''
			}
		},
		watch: {
			file_path (nv) {
				if (nv && nv !== '') {
					this.handleDownload(nv);
				}
			}
		},
		methods: {
			handleDownload(url) {
				const _this = this;
				_this.show = true;
				const downloadTask = uni.downloadFile({
					url,
					success: (res) => {
						_this.downLoad = false;
						if (res.statusCode === 200) {
							uni.saveFile({
							  tempFilePath: res.tempFilePath,
							  success: (res2) => {
								  var savedFilePath = res.tempFilePath;
									uni.openDocument({
										filePath: savedFilePath,
										success: (res3) => {
											console.log(res3, 777);
										},
										fail: (res3) => {
											console.log(res3, 555);
										}
									});
							  }
							});
						}
					}, fail: (res) => {
						_this.downLoad = false;
					}
				});
				downloadTask.onProgressUpdate(res => {
					this.percent = res.progress;
					if (res.progress >= 100) {
						setTimeout(() => {
							this.show = false;
						}, 500)
					}
				});
			}
		}
	}
</script>
<style lang="scss">
	
</style>